package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


/**
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.02.23
 * @since 1.0
 */
@RestController
@RequestMapping("/admin/storage")
public class DtsAdminStorageController {

    @Autowired
    DtsStorageService storageService;


    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsStorage storage){
       storageService.delete(storage);
       return ResponseUtil.ok();
    }

    @RequestMapping("/update")
    public Object update(@RequestBody DtsStorage storage){
       DtsStorage storage1 =  storageService.update(storage);

        return ResponseUtil.ok(storage1);

    }

    @GetMapping("/list")
    public Object list(
        @RequestParam(value = "page",required = false,defaultValue = "1") Integer page ,
        @RequestParam(value = "limit",required = false,defaultValue = "10")Integer limit ,
        String name , String key ,String sort, String order){
       IPage<DtsStorage> iPage =  storageService.queryPage(page,limit,name,key,sort,order);

        Map<String,Object> data = new HashMap<>();

        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());

       return ResponseUtil.ok(data);
    }


    @PostMapping("/create")
    public Object upload(MultipartFile file) throws Exception {

        String filename = file.getOriginalFilename();
        byte[] bytes = file.getBytes();
        storageService.upload(filename,bytes,(int)file.getSize());
        Object upload =  storageService.upload(filename,bytes,(int)file.getSize());
        return ResponseUtil.ok(upload);
    }
}
